﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ueditortest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private dynamic scriptObject = null;
        ObjectForScriptingHelper helper = null;
        public MainWindow()
        {
            InitializeComponent();
            helper = new ObjectForScriptingHelper(this);
            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.webb.Source = new Uri(@"F:\工作\ueditortest\ueditortest\utf8-net\index.html", UriKind.RelativeOrAbsolute);
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string[] objects = new string[1];
            objects[0] = box.Text;
            webb.InvokeScript("setContent", objects);
             string  cmd = "http://www.w3school.com.cn/i/eg_chinarose.jpg";
            string img = "<img src=\"" + cmd + "\"/>";
            string[] objects2 = new string[1];
            objects2[0] = img;
            webb.InvokeScript("insertHtml", objects2);
        }

        private void webb_LoadCompleted(object sender, NavigationEventArgs e)
        {
            this.webb.ObjectForScripting = helper;
           
        }

        private void get_Click(object sender, RoutedEventArgs e)
        {
            //if (!BrowserInteropHelper.IsBrowserHosted)
            //{
            //    MessageBox.Show("不满足与JS调用条件");
            //    return;
            //}
            //scriptObject = BrowserInteropHelper.HostScript;
            //if (scriptObject != null)
            //{
            //    scriptObject.SetWpfObj(new CallbackClass());
            //}
            
           var df=  webb.InvokeScript("getContent");
           MessageBox.Show(df.ToString());
        }
    }
    //记得加上这个特性
    //[ComVisible(true)]
    //public class CallbackClass
    //{
    //    public string MyMethod(string message)
    //    {
    //        return "来自WPF的中转函数，" + message;
    //    }
    //}
  
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问 
  
    public class ObjectForScriptingHelper
    {
        MainWindow mainWindow;

        public ObjectForScriptingHelper(MainWindow main)
        {
            mainWindow = main;
        }
        public ObjectForScriptingHelper()
        {
        }
       
        //这个方法就是网页上要反问的方法
        public void ShowMsg(string cmd)
        {
            cmd = "http://www.w3school.com.cn/i/eg_chinarose.jpg";
            string img = "<img src=\"" + cmd + "\"/>";
            string[] objects = new string[1];
            objects[0] = img;
            mainWindow.webb.InvokeScript("insertHtml", objects);
            MessageBox.Show(cmd);

        }
    }
}
